package com.ayz.gateway.springbootstarter.application;

import cn.hutool.log.Log;

import com.ayz.gateway.sdk.app.AppSdkClientService;
import lombok.Getter;
import lombok.Setter;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;

/**
 * @ClassName GatewaySdkStartListener
 * @Description TODO:监听Spring容器上下文刷新事件
 *                   完成SDK客户端网络通信组件的启动
 * @Author 快乐的星球
 * @Date 2023/6/3 16:40
 * @Version 1.0
 **/
@Getter
@Setter
public class GatewaySdkStartListener implements ApplicationListener<ContextRefreshedEvent> {
    /**
     * 网关应用程序SDK网络通信组件
     */
    private AppSdkClientService appSdkClientService;

    private static final Log LOG=Log.get(GatewaySdkStartListener.class);

    public GatewaySdkStartListener(AppSdkClientService appSdkClientService) {
        this.appSdkClientService = appSdkClientService;
    }


    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        LOG.info("API网关SDK网络通信组件启动.....");
        try {
            this.appSdkClientService.start();
            LOG.info("API网关SDK网络通信组件启动成功!");
        }catch (Exception ex){
            LOG.error("网络通信组件启动失败!");
            throw ex;
        }
    }
}
